import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.text.JTextComponent;


@SuppressWarnings("serial")
public class OnScreenKeyboard extends JPanel {
	
	private boolean shift;
	JButton buttons[] = new JButton[100];
	private String insertion;
	private String focusedFieldText;
	protected int cursorPosition;
	
	
	
	public OnScreenKeyboard(final JTextComponent focusedField, final int caretPosition, 
			final boolean shiftStatus, final JPanel backgroundPanel) {
		
		shift = shiftStatus;
		cursorPosition = caretPosition;
		setOpaque(false);
		setBackground(Color.black);
		setPreferredSize(new Dimension(Global.displayX*51/81, Global.displayY*26/81));
		setLayout(new XYLayout());
		
		focusedField.setCaretPosition(caretPosition);
		focusedFieldText = focusedField.getText();
		System.out.println(focusedFieldText + caretPosition);
		
		Dimension dim = new Dimension(Global.displayX*4/81, Global.displayY*4/81);
		
		int fontSize = Global.displayX/100;
		
		int row1 = Global.displayY*1/81;
		int row2 = Global.displayY*6/81;
		int row3 = Global.displayY*11/81;
		int row4 = Global.displayY*16/81;
		int row5 = Global.displayY*21/81;
		
		int offset = Global.displayX*2/81;
		
		
		for (int i = 0; i < 10; i++) {
			//Brojevi 0 - 9
			if (shift == false) {
				buttons[i] = new NumPadButtonLight("" + (char) (i + 48), fontSize);

			} else {
				buttons[i] = new NumPadButtonLight("" + (char) (i + 32), fontSize);
				buttons[0] = new NumPadButtonLight("" + (char) (47), fontSize);
			}

		}
		
		for (int i = 10; i < 36; i++) {
			//Slova A - Z
			if (shift == false) {
				buttons[i] = new NumPadButtonLight("" + (char) (i + 87), fontSize);

			} else {
				buttons[i] = new NumPadButtonLight("" + (char) (i + 55), fontSize);
			}

			buttons[i].addMouseListener(new java.awt.event.MouseAdapter() {
				public void mousePressed(java.awt.event.MouseEvent evt) {
				}
			});
		}
		
		//Specijalni znakovi
		if (shift == false) {
			buttons[36] = new NumPadButtonLight("" + (char) (46), fontSize);
			buttons[37] = new NumPadButtonLight("" + (char) (44), fontSize);
			buttons[39] = new NumPadButtonLight("" + (char) (63), fontSize);
			buttons[42] = new NumPadButtonLight("" + (char) (43), fontSize);
		} else {
			buttons[36] = new NumPadButtonLight("" + (char) (58), fontSize);
			buttons[37] = new NumPadButtonLight("" + (char) (59), fontSize);
			buttons[39] = new NumPadButtonLight("" + (char) (95), fontSize);
			buttons[42] = new NumPadButtonLight("" + (char) (45), fontSize);
		}
		
		buttons[38] = new StandardButton("Aa", Global.displayX/120);
		buttons[40] = new RedButton("Del", Global.displayX/120);
		buttons[41] = new StandardButton("\u2193", fontSize);
		buttons[43] = new NumPadButtonLight("" + (char) (32), fontSize);


		buttons[36].addMouseListener(new java.awt.event.MouseAdapter() {
			public void mousePressed(java.awt.event.MouseEvent evt) {
			}
		});
		
		buttons[1].setLocation(Global.displayX*1/81, row1);//1
		buttons[2].setLocation(Global.displayX*6/81, row1);//2
		buttons[3].setLocation(Global.displayX*11/81, row1);//3
		buttons[4].setLocation(Global.displayX*16/81, row1);//4
		buttons[5].setLocation(Global.displayX*21/81, row1);//5
		buttons[6].setLocation(Global.displayX*26/81, row1);//6
		buttons[7].setLocation(Global.displayX*31/81, row1);//7
		buttons[8].setLocation(Global.displayX*36/81, row1);//8
		buttons[9].setLocation(Global.displayX*41/81, row1);//9
		buttons[0].setLocation(Global.displayX*46/81, row1);//0
		buttons[10].setLocation(Global.displayX*1/81 + offset, row3);//A
		buttons[11].setLocation(Global.displayX*21/81 + offset, row4);//B
		buttons[12].setLocation(Global.displayX*11/81 + offset, row4);//C
		buttons[13].setLocation(Global.displayX*11/81 + offset, row3);//D
		buttons[14].setLocation(Global.displayX*11/81, row2);//E
		buttons[15].setLocation(Global.displayX*16/81 + offset, row3);//F
		buttons[16].setLocation(Global.displayX*21/81 + offset, row3);//G
		buttons[17].setLocation(Global.displayX*26/81 + offset, row3);//H
		buttons[18].setLocation(Global.displayX*36/81, row2);//I
		buttons[19].setLocation(Global.displayX*31/81 + offset, row3);//J
		buttons[20].setLocation(Global.displayX*36/81 + offset, row3);//K
		buttons[21].setLocation(Global.displayX*41/81 + offset, row3);//L
		buttons[22].setLocation(Global.displayX*31/81 + offset, row4);//M
		buttons[23].setLocation(Global.displayX*26/81 + offset, row4);//N
		buttons[24].setLocation(Global.displayX*41/81, row2);//O
		buttons[25].setLocation(Global.displayX*46/81, row2);//P
		buttons[26].setLocation(Global.displayX*1/81, row2);//Q
		buttons[27].setLocation(Global.displayX*16/81, row2);//R
		buttons[28].setLocation(Global.displayX*6/81 + offset, row3);//S
		buttons[29].setLocation(Global.displayX*21/81, row2);//T
		buttons[30].setLocation(Global.displayX*31/81, row2);//U
		buttons[31].setLocation(Global.displayX*16/81 + offset, row4);//V
		buttons[32].setLocation(Global.displayX*6/81, row2);//W
		buttons[33].setLocation(Global.displayX*6/81 + offset, row4);//X
		buttons[34].setLocation(Global.displayX*1/81 + offset, row4);//Y
		buttons[35].setLocation(Global.displayX*26/81, row2);//Z
		buttons[36].setLocation(Global.displayX*41/81, row5);//.:
		buttons[37].setLocation(Global.displayX*11/81, row5);//,;
		buttons[38].setLocation(Global.displayX*6/81, row5);//Shift
		buttons[39].setLocation(Global.displayX*46/81, row5);//?_
		buttons[40].setLocation(Global.displayX*41/81 + offset, row4);//Delete
		buttons[41].setLocation(Global.displayX*1/81, row5);//Close
		buttons[42].setLocation(Global.displayX*36/81 + offset, row4);//+-
		buttons[43].setLocation(Global.displayX*16/81, row5);//Space





		
		for (int i = 0; i < 43; i++){
			buttons[i].setPreferredSize(dim);
			this.add(buttons[i]);
		}
		buttons[43].setPreferredSize(new Dimension(Global.displayX*24/81, Global.displayY*4/81));
		this.add(buttons[43]);
		
		
		for (int i = 0; i < 44; i++){
			final int k = i;
			if (i != 41 & i != 38 & i != 40)
			buttons[i].addMouseListener(new java.awt.event.MouseAdapter() {
				public void mousePressed(java.awt.event.MouseEvent evt) {
					insertion = buttons[k].getText();
					System.out.println(insertion);
					focusedFieldText = focusedFieldText.substring(0, cursorPosition) + insertion 
							+ focusedFieldText.substring(cursorPosition, focusedFieldText.length());
					System.out.println(focusedFieldText);
					cursorPosition++;
					focusedField.setText(focusedFieldText);
				}
			});
		}
		
		buttons[38].addMouseListener(new java.awt.event.MouseAdapter() {
			public void mousePressed(java.awt.event.MouseEvent evt) {
				shift = true;
        		backgroundPanel.removeAll();
        		backgroundPanel.add(new OnScreenKeyboard(focusedField, cursorPosition, !shiftStatus, backgroundPanel));
        		backgroundPanel.revalidate();
			}
		});
		
		buttons[41].addMouseListener(new java.awt.event.MouseAdapter() {
			public void mousePressed(java.awt.event.MouseEvent evt) {
				Component c = (Component) evt.getSource();
				Component temp = SwingUtilities.getRoot(c);
				JFrame keyFrame = (JFrame) temp;
				keyFrame.dispose();
			}
		});
		
		buttons[40].addMouseListener(new java.awt.event.MouseAdapter() {
			public void mousePressed(java.awt.event.MouseEvent evt) {
				if (focusedFieldText.length() > 0){
					if (cursorPosition == 0)
						cursorPosition++;
					else
						cursorPosition --;
					StringBuffer sb = new StringBuffer(focusedFieldText);
					sb.deleteCharAt(cursorPosition);
					focusedFieldText = sb.toString();
					focusedField.setText(focusedFieldText);	
				}

				
			}
		});
		
		
	}
	

	
	
	
	public void paintComponent(Graphics g) {
		Graphics2D g2 = (Graphics2D)g;
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		super.paintComponent (g);
		g2.setColor(new Color(50, 50, 50));
		g2.fillRoundRect(0, 0, Global.displayX*51/81, Global.displayY*26/81, 10, 10);
		g2.setColor(new Color(120, 120, 120));
		g2.drawRoundRect(2, 2, Global.displayX*51/81 - 5, Global.displayY*26/81 - 5, 10, 10);
		
		
	}
	

}
